home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / tdepth.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  9KB  |  329 lines

  1. /*
  2.  * (c) Copyright 1993-94, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <GL/glx.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <X11/keysym.h>
  42.  
  43. static int RGBattributes[] = {
  44.     GLX_RGBA,
  45.     GLX_RED_SIZE, 1,
  46.     GLX_GREEN_SIZE, 1,
  47.     GLX_BLUE_SIZE, 1,
  48.     GLX_DEPTH_SIZE, 1,
  49.     None,
  50. };
  51.  
  52. static int CIattributes[] = {
  53.     GLX_DEPTH_SIZE, 1,
  54.     None,
  55. };
  56.  
  57. enum {
  58.     BLACK = 0,
  59.     RED,
  60.     GREEN,
  61.     YELLOW,
  62.     BLUE,
  63.     MAGENTA,
  64.     CYAN,
  65.     WHITE
  66. };
  67.  
  68. #define COLOR_OFFSET_1    16
  69. #define COLOR_OFFSET_2    32
  70.  
  71.  
  72. static float rgbMap[][3] = {
  73.     {0, 0, 0},
  74.     {1, 0, 0},
  75.     {0, 1, 0},
  76.     {1, 1, 0},
  77.     {0, 0, 1},
  78.     {1, 0, 1},
  79.     {0, 1, 1},
  80.     {1, 1, 1}
  81. };
  82.  
  83. static long W = 300, H = 300;
  84. static long size, mode, rgb;
  85. static long antiAlias = 0;
  86. static long stipple = 0;
  87. static long ci1 = BLUE, ci2 = GREEN;
  88. static unsigned char stippleBits[32*4] = {
  89.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  90.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  91.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  92.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  93.  
  94.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  95.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  96.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  97.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  98.  
  99.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  100.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  101.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  102.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  103.  
  104.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  105.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  106.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  107.     0xAA, 0xAA, 0xAA, 0xAA,    0x55, 0x55, 0x55, 0x55,
  108. };
  109.  
  110.  
  111. static void DoDisplay(void)
  112. {
  113.     glEnable(GL_SCISSOR_TEST);
  114.     glEnable(GL_DEPTH_TEST);
  115.  
  116.     glViewport(10, 10, W-20, H-20);
  117.     glScissor(10, 10, W-20, H-20);
  118.  
  119.     glMatrixMode(GL_PROJECTION);
  120.     glLoadIdentity();
  121.     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  122.     glMatrixMode(GL_MODELVIEW);
  123.  
  124.     glClearColor(0.0, 0.0, 0.0, 0.0);
  125.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  126.  
  127.     if (antiAlias) {
  128.     glEnable(GL_POLYGON_SMOOTH);
  129.     glEnable(GL_BLEND);
  130.     glDisable(GL_DEPTH_TEST);
  131.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  132.     } else {
  133.     glDisable(GL_POLYGON_SMOOTH);
  134.     glDisable(GL_BLEND);
  135.     glEnable(GL_DEPTH_TEST);
  136.     }
  137.     if (stipple) {
  138.     glPolygonStipple(stippleBits);
  139.     glEnable(GL_POLYGON_STIPPLE);
  140.     } else {
  141.     glDisable(GL_POLYGON_STIPPLE);
  142.     }
  143.  
  144.     /*
  145.     ** Draw two triangles that intersect each other.
  146.     */
  147.     glBegin(GL_TRIANGLES);
  148.         rgb ? glColor3fv(rgbMap[BLUE]) : glIndexi(ci1);
  149.     glVertex3f( 0.9, -0.9, -30.0);
  150.     glVertex3f( 0.9,  0.9, -30.0);
  151.     glVertex3f(-0.9,  0.0, -30.0);
  152.     rgb ? glColor3fv(rgbMap[GREEN]) : glIndexi(ci2);
  153.     glVertex3f(-0.9, -0.9, -40.0);
  154.     glVertex3f(-0.9,  0.9, -40.0);
  155.     glVertex3f( 0.9,  0.0, -25.0);
  156.     glEnd();
  157.  
  158.     glFlush();
  159. }
  160.  
  161. static void Usage(void)
  162. {
  163.     printf("Usage: tdepth [-c]\n");
  164.     printf("   -c:  Run in color index mode\n");
  165.     exit(-1);
  166. }
  167.  
  168. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  169. {
  170.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  171.     return GL_TRUE;
  172.     }
  173.     return GL_FALSE;
  174. }
  175.  
  176. int main(long argc, char** argv)
  177. {
  178.     XVisualInfo *vi;
  179.     Display *dpy;
  180.     Colormap cmap;
  181.     Window window;
  182.     XSetWindowAttributes swa;
  183.     GLXContext cx;
  184.     XEvent event;
  185.     GLboolean needDisplay;
  186.     int i;
  187.  
  188.     rgb = 1;
  189.     for (i = 1; i < argc; i++) {
  190.         if (argv[i][0] == '-') {
  191.             switch (argv[i][1]) {
  192.               case 'c':
  193.                 rgb = 0;
  194.                 break;
  195.               default:
  196.                 Usage();
  197.             }
  198.         } else {
  199.             Usage();
  200.         }
  201.     }
  202.  
  203.     dpy = XOpenDisplay(0);
  204.     if (!dpy) {
  205.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  206.     return -1;
  207.     }
  208.  
  209.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  210.              rgb ? RGBattributes : CIattributes);
  211.     if (!vi) {
  212.     fprintf(stderr, "No appropriate visual on \"%s\"\n",
  213.         getenv("DISPLAY"));
  214.     return -1;
  215.     }
  216.  
  217.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  218.                rgb ? AllocNone : AllocAll);
  219.     swa.border_pixel = 0;
  220.     swa.colormap = cmap;
  221.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  222.     | KeyReleaseMask;
  223.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  224.                W, H,
  225.                0, vi->depth, InputOutput, vi->visual,
  226.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  227.     XSetWMColormapWindows(dpy, window, &window, 1);
  228.     XMapWindow(dpy, window);
  229.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  230.  
  231.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  232.     if (!glXMakeCurrent(dpy, window, cx)) {
  233.     fprintf(stderr, "Can't make window current to context\n");
  234.     return -1;
  235.     }
  236.  
  237.     if (!rgb) {
  238.     XColor xc[32+2];
  239.  
  240.     /* Build two color ramps to use */
  241.     unsigned long pixel;
  242.     unsigned short red, green, blue;
  243.     char flags;  /* do_red, do_green, do_blue */
  244.     char pad;
  245.  
  246.     for (i = 0; i < 16; i++) {
  247.         xc[i].pixel = i + COLOR_OFFSET_1;
  248.         xc[i].red = 0;
  249.         xc[i].green = 0;
  250.         xc[i].blue = (unsigned short) (65535.0 * (i / 15.0));
  251.         xc[i].flags = DoRed | DoGreen | DoBlue;
  252.  
  253.         xc[i+16].pixel = i + COLOR_OFFSET_2;
  254.         xc[i+16].red = 0;
  255.         xc[i+16].green = (unsigned short) (65535.0 * (i / 15.0));
  256.         xc[i+16].blue = 0;
  257.         xc[i+16].flags = DoRed | DoGreen | DoBlue;
  258.     }
  259.  
  260.     xc[32].pixel = BLUE;
  261.     xc[32].red = xc[32].green = 0;
  262.     xc[32].blue = 65535;
  263.     xc[32].flags = DoRed|DoGreen|DoBlue;
  264.  
  265.     xc[32].pixel = GREEN;
  266.     xc[32].red = xc[32].blue = 0;
  267.     xc[32].green = 65535;
  268.     xc[32].flags = DoRed|DoGreen|DoBlue;
  269.     XStoreColors(dpy, cmap, xc, 34);
  270.     }
  271.  
  272.     mode = 0;
  273.     size = 1;
  274.  
  275.     needDisplay = GL_TRUE;
  276.     for (;;) {
  277.     do {
  278.         XNextEvent(dpy, &event);
  279.         switch (event.type) {
  280.           case Expose:
  281.         needDisplay = GL_TRUE;
  282.         break;
  283.           case ConfigureNotify:
  284.         W = event.xconfigure.width;
  285.         H = event.xconfigure.height;
  286.         needDisplay = GL_TRUE;
  287.         break;
  288.           case KeyPress:
  289.         {
  290.             char buf[100];
  291.             int rv;
  292.             KeySym ks;
  293.  
  294.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  295.             switch (ks) {
  296.               case XK_A:
  297.               case XK_a:
  298.             antiAlias = !antiAlias;
  299.             if (!rgb) {
  300.                 if (antiAlias) {
  301.                 ci1 = COLOR_OFFSET_1;
  302.                 ci2 = COLOR_OFFSET_2;
  303.                 } else {
  304.                 ci1 = BLUE;
  305.                 ci2 = GREEN;
  306.                 }
  307.             }
  308.             needDisplay = GL_TRUE;
  309.             break;
  310.               case XK_S:
  311.               case XK_s:
  312.             stipple = !stipple;
  313.             needDisplay = GL_TRUE;
  314.             break;
  315.               case XK_Escape:
  316.             return 0;
  317.             }
  318.         }
  319.         break;
  320.         }
  321.     } while (XPending(dpy) != 0);
  322.  
  323.     if (needDisplay) {
  324.         needDisplay = GL_FALSE;
  325.         DoDisplay();
  326.     }
  327.     }
  328. }
  329.